{%mainunit bitcoin_p2p.pas}

{$IFDEF INTERFACE}
type
  { TMsgAddr is used to transport the knowledge of known peer IP addresses
    between the peers. When we receive such messages we will add these
    addresses to our peerfinder database. We should also broadcast them
    to other connected peers when we encounter a previously unknown address.}
    {$note I should implement what I wrote above}
  TMsgAddr = class(TMsg)
    Count     : Integer;
    Addresses : array of TNetAddress;
    constructor Create;
    destructor Destroy; override;
    procedure ParseAndProcess; override;
    function Serialize: String; override;
    class function GetCommand: Tp2pCommand; override;
  end;

{$ENDIF}
{$IFDEF IMPLEMENTATION}

constructor TMsgAddr.Create;
begin
  //
end;

destructor TMsgAddr.Destroy;
var
  I : Integer;
begin
  for I := 0 to Count-1 do
    Addresses[i].Free;
  SetLength(Addresses, 0);
  inherited Destroy;
end;

procedure TMsgAddr.ParseAndProcess;
var
  C           : Integer;
  I           : Integer;
begin
  Count := 0;
  C := ReadVarInt;
  SetLength(Addresses, C);
  for I := 0 to C-1 do begin

    if FPeer.Version >= 31402 then begin // angry rant
      // This UGLY wart is an outright harassment to everybody implementing
      // this protocol. They could have simply made this timestamp part of
      // the net_addr structure (TNetAddress) itself so it would be used
      // whenever an address is transmitted anywhere but they only hacked it
      // into this addr message, probably to "get it done quickly".
      // Furthermore in other places they use 64bit for a timestamp but for
      // some reason here they suddenly choose 32bit. I wonder what all this
      // VarInt stuff is all about when it is not used consequently?
      // I can see already the next if statement occuring here in a few years
      // to make it a 64bit (or a varint) depending on the protocol version.
      // For now I am simply going to ignore and throw away this timestamp,
      // at least until I have cooled down a bit again.
      ReadDWord;
    end; // of rant

    Addresses[I] := TNetAddress.Create(FPeer, FReceiveData);
    Inc(Count);
    Addresses[I].ParseAndProcess;
  end;
end;

function TMsgAddr.Serialize: String;
begin
  {$warning implement this}
  Result := ''
end;

class function TMsgAddr.GetCommand: Tp2pCommand;
begin
  Result := 'addr'#0#0#0#0#0#0#0#0;
end;

{$ENDIF}
